package com.senyin.dao;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.entity.StaffInfo;
import com.senyin.utils.ComMethods;
import com.senyin.utils.JDBCUtil;
import com.senyin.utils.UUIDUtil;
import org.junit.jupiter.api.Test;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static com.senyin.constant.UserConstant.DEFAULT_ROLE;
import static com.senyin.constant.UserConstant.USER_LOGIN_STATE;

public class StaffInfoDao {
    public BaseResponse<StaffInfo> login(String siId, String password, HttpServletRequest request) throws SQLException {
        Connection conn = JDBCUtil.getConn();
        System.out.println(conn);
        Statement statement = conn.createStatement();
        String sql = "select * from staff_info where si_id='"+siId
                +"' and si_pass='"+password+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        StaffInfo staffInfo = null;
        if (resultSet.next()){
            staffInfo = new StaffInfo();
            staffInfo.setSiUuid(resultSet.getString("si_uuid"));
            staffInfo.setSiId(resultSet.getString("si_id"));
            staffInfo.setSiName(resultSet.getString("si_name"));
            staffInfo.setSiSex(resultSet.getInt("si_sex"));
            staffInfo.setSiBirth(resultSet.getDate("si_birth"));
            staffInfo.setPsId(resultSet.getString("ps_id"));
            staffInfo.setDpId(resultSet.getString("dp_id"));
            staffInfo.setSiAuth(resultSet.getInt("si_auth"));
        }
        if (staffInfo!=null){
            //存储登录态
            request.getSession().setAttribute(USER_LOGIN_STATE,staffInfo);
            return ResultUtils.success(staffInfo);
        }
        return ResultUtils.error(ErrorCode.PARAMS_ERROR,"账号不存在");
    }

    @Test
    public void test() throws SQLException {
        Connection conn = JDBCUtil.getConn();
        System.out.println(conn);
    }

    public BaseResponse addStaff(StaffInfo staffInfo,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (Objects.isNull(attribute)){
            return ResultUtils.error(ErrorCode.NO_AUTH,"登录过期或未登录");
        }
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH,"普通用户无权限");
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String siId = staffInfo.getSiId();
        String siName = staffInfo.getSiName();
        Integer siSex = staffInfo.getSiSex();
        Date siBirth = staffInfo.getSiBirth();
        String psId = staffInfo.getPsId();
        String dpId = staffInfo.getDpId();
        String siPass = staffInfo.getSiPass();
        Integer siAuth = staffInfo.getSiAuth();
        String check = "select * from staff_info where si_id='"+siId+"'";
        ResultSet resultSet = statement.executeQuery(check);
        if (resultSet.next()){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"工号重复，请重新输入");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String add = "insert into staff_info values('"+ UUIDUtil.get8UUID()
                +"','"+siId+"','"+siName+"',"+siSex+",'"+sdf.format(siBirth)+"','"+psId+"','"+dpId
                +"','"+siPass+"',"+siAuth+")";
        int i = statement.executeUpdate(add);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"添加失败");
    }

    public BaseResponse<List<StaffInfo>> showAll() throws SQLException {
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String sql = "select * from staff_info";
        ResultSet resultSet = statement.executeQuery(sql);
        ArrayList<StaffInfo> staffInfos = new ArrayList<>();
        while (resultSet.next()){
            StaffInfo staffInfo = new StaffInfo();
            staffInfo.setSiUuid(resultSet.getString("si_uuid"));
            staffInfo.setSiId(resultSet.getString("si_id"));
            staffInfo.setSiName(resultSet.getString("si_name"));
            staffInfo.setSiSex(resultSet.getInt("si_sex"));
            staffInfo.setSiBirth(resultSet.getDate("si_birth"));
            staffInfo.setPsId(resultSet.getString("ps_id"));
            staffInfo.setDpId(resultSet.getString("dp_id"));
            staffInfo.setSiAuth(resultSet.getInt("si_auth"));
            staffInfos.add(staffInfo);
        }
        if (staffInfos.size()<=0){
            return ResultUtils.error(ErrorCode.NULL_ERROR,"无数据");
        }
        return ResultUtils.success(staffInfos,staffInfos.size());
    }

    public BaseResponse modStaff(StaffInfo staffInfo,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH);
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String checkId = "select * from staff_info where si_id='"+staffInfo.getSiId()+"'";
        ResultSet resultSet = statement.executeQuery(checkId);
        if (resultSet.next()){
            if (!staffInfo.getSiUuid().equals(resultSet.getString("si_uuid"))) {
                return ResultUtils.error(ErrorCode.PARAMS_ERROR,"员工工号已存在");
            }
        }
        String mod = "update staff_info set si_id='"+staffInfo.getSiId()
                +"',si_name='"+staffInfo.getSiName()
                +"',si_sex="+staffInfo.getSiSex()
                +",si_birth='"+ComMethods.dateFormat(staffInfo.getSiBirth())
                +"',ps_id='"+staffInfo.getPsId()
                +"',dp_id='"+staffInfo.getDpId()
                +"',si_auth="+staffInfo.getSiAuth()
                +" where si_uuid='"+staffInfo.getSiUuid()+"'";
        int i = statement.executeUpdate(mod);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

    public BaseResponse delStaffByUUID(String siUUID,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH);
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String sql = "delete from staff_info where si_uuid='"+siUUID+"'";
        int i = statement.executeUpdate(sql);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

//    public BaseResponse search(HashMap map){
//        Connection conn = JDBCUtil.getConn();
//        Statement statement = conn.createStatement();
//        //根据 工号、姓名、性别、岗位、部门查询、权限
//        if (map.)
//    }


}
